<template>
	<view class="content">
		<view class="notice" v-if="text">
			<image src="@/static/img/err.png" mode="widthFix"></image>
			<uni-notice-bar scrollable background-color="#FFF3EE" :speed="speed" color="#BC7A25" :text="text" />
		</view>
		<view class="info">
			<view class="title">请上传身份证的正反面</view>
			<view class="img_bg">
				<image v-if="!img_1" src="@/static/img/aut_img1.png" @click="changeImg(1)" mode="widthFix"></image>
				<image v-else :src="img_1" @click="changeImg(1)" mode="aspectFit" class="img_demo"></image>
			</view>
			<view class="text">上传身份证头像面</view>
			<view class="img_bg">
				<image v-if="!img_2" src="@/static/img/aut_img2.png" @click="changeImg(2)" mode="widthFix"></image>
				<image v-else :src="img_2" @click="changeImg(2)" mode="aspectFit" class="img_demo"></image>
			</view>
			<view class="text">上传身份证国徽面</view>
		</view>
		<view class="info">
			<view class="title">请完善您的个人信息</view>
			<view class="item">
				<view>姓名</view>
				<input type="text" placeholder="请输入真实姓名" v-model="name" />
			</view>
			<view class="item">
				<view>身份证号</view>
				<input type="text" placeholder="请输入本人身份证号" v-model="card" />
			</view>
		</view>
		<view v-if="info.is_upload==1&&(info.passed==0||info.passed==1)" class="default_btn btn color_9" @click="hint">
			提交审核</view>
		<view v-else class="default_btn btn" @click="edit">提交审核</view>
	</view>
</template>

<script>
	import {
		fileUp,
		queryCertificate,
		editCertificate
	} from '@/common/js/index.js'
	export default {
		data() {
			return {
				img_1: '',
				img_2: '',
				userId: '',
				name: '',
				card: '',
				text: '',
				speed: 50,
				info: '',
			}
		},
		onLoad(e) {
			if(e.info) {
				this.info = JSON.parse(e.info)
			}
			this.userId = e.id
			console.log(this.info);
			console.log(e.id);
			this.query()
		},
		methods: {
			hint() {
				var text
				if (this.info.passed == 0) text = '审核中…'
				if (this.info.passed == 1) text = '已上传'
				uni.showToast({
					title: text,
					icon: 'none',
					duration: 2000
				})
			},
			query() {
				queryCertificate({
					type_id: 1,
					user_id: this.userId
				}).then(res => {
					this.img_1 = res.data.images[0]
					this.img_2 = res.data.images[1]
					this.name = res.data.user_info.real_name
					this.card = res.data.user_info.idno
					this.text = res.data.msg
					if(!this.info) {
						this.info = res.data
					}
				})
			},
			edit() {
				if(!this.img_1||!this.img_2) {
					uni.showToast({
						title:'请上传完整信息',
						icon:'none'
					})
					return
				}
				editCertificate({
					type_id: 1,
					user_id: this.userId,
					images: JSON.stringify([this.img_1 || '', this.img_2 || '']),
					real_name: this.name,
					idno: this.card
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 2000)
					}
					console.log(res);
				})
			},
			changeImg(num) {
				if (this.info.is_upload == 1 && (this.info.passed == 0 || this.info.passed == 1)) {
					this.hint()
					return
				}
				console.log(this.userId);
				var that = this
				uni.chooseImage({
					count: 1,
					sizeType: ["compressed"],
					success(response) {
						console.log(response);
						// 选择图片后, 返回的数据
						var fileUrl = response.tempFilePaths[0]
						console.log(fileUrl);
						fileUp({
							file: fileUrl,
							info: {
								type_id: 1,
								user_id: that.userId
							}
						}).then(res => {
							var info = JSON.parse(res)
							console.log(num);
							if (info.code == 200) {
								if (num == 1) that.img_1 = info.data[0]
								if (num == 2) that.img_2 = info.data[0]
								console.log(that.img_1);
								// that.save()
							} else {
								uni.showToast({
									title: info.msg,
									icon: 'error',
									duration: 3000
								})
							}
						})
					}
				})
			}
		},
	}
</script>

<style scoped lang="scss">
	.content {
		/* background: white; */
		position: relative;
		overflow: hidden;
	}

	.info {
		margin: 40rpx 30rpx;
		padding: 40rpx 30rpx 60rpx;
		background-color: white;
		background: #FFFFFF;
		border-radius: 12rpx;
		overflow: hidden;

		.title {
			font-weight: 500;
			font-size: 32rpx;
		}

		.text {
			text-align: center;
			margin-bottom: 40rpx;
		}

		.img_bg {
			width: 630rpx;
			height: 376rpx;
			background: #F9F9F9;
			border-radius: 12rpx;
			margin: 30rpx 0;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 401rpx;
			}

			.img_demo {
				width: 401rpx !important;
				height: 283rpx !important;
				margin: 0 !important;
				// position: absolute;
				// top: 0;
				// left: 0;
			}
		}

		.item {
			height: 110rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #EEEEEE;

			input {
				font-size: 26rpx;
				text-align: right;
			}
		}
	}

	.btn {
		margin: 80rpx auto 100rpx;
	}

	.color_9 {
		background: #999999;
	}

	/deep/ .uni-noticebar {
		width: 690rpx;
		background-color: #FFF3EE;
		border-radius: 59rpx;
		padding-left: 80rpx;
	}

	.notice {
		margin: 40rpx 30rpx;
		position: relative;

		image {
			position: absolute;
			left: 20rpx;
			top: 12rpx;
			width: 48rpx;
		}
	}
</style>